import { type RouteRecordRaw, createRouter } from "vue-router"
import { history, flatMultiLevelRoutes } from "./helper"
import routeSettings from "@/config/route"
import { getCookie } from "@/utils/cache/cookies"
const Layouts = () => import("@/layouts/index.vue")
const cookiestringfy = getCookie("userinfo") || "{}"
const cookie = JSON.parse(cookiestringfy)
const titleTag = {
  admin: "情报",
  admin_yl: "医疗",
  admin_yw: "运维"
}
/**
 * 常驻路由
 * 除了 redirect/403/404/login 等隐藏页面，其他页面建议设置 Name 属性
 */
export const constantRoutes: RouteRecordRaw[] = [
  {
    path: "/redirect",
    component: Layouts,
    meta: {
      hidden: true
    },
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/redirect/index.vue")
      }
    ]
  },
  {
    path: "/403",
    component: () => import("@/views/error-page/403.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/404",
    component: () => import("@/views/error-page/404.vue"),
    meta: {
      hidden: true
    },
    alias: "/:pathMatch(.*)*"
  },
  {
    path: "/login",
    component: () => import("@/views/login/index.vue"),
    meta: {
      hidden: true
    }
  },
  {
    path: "/",
    component: Layouts,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        component: () => import("@/views/dashboard/index.vue"),
        name: "Dashboard",
        meta: {
          title: "首页",
          svgIcon: "dashboard",
          affix: true
        }
      }
    ]
  },
  {
    path: "/file-manager",
    component: Layouts,
    redirect: "/file-manager",
    name: "File",
    meta: {
      title: `${titleTag[cookie.role]}管理`,
      elIcon: "Folder"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/File-Manager/index.vue"),
        name: "FileManager",
        meta: {
          title: `${titleTag[cookie.role]}管理`
        }
      }
    ]
  },
  {
    path: "/AI",
    component: Layouts,
    redirect: "/AI",
    name: "AI",
    meta: {
      title: `AI${titleTag[cookie.role]}助手`,
      elIcon: "UserFilled"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/AI/index.vue"),
        name: "UserFilled",
        meta: {
          title: `AI${titleTag[cookie.role]}助手`
        }
      }
    ]
  },
  {
    path: "/config",
    component: Layouts,
    redirect: "/config/prompt",
    name: "Config",
    meta: {
      title: "配置",
      elIcon: "UserFilled"
    },
    children: [
      {
        path: "prompt",
        component: () => import("@/views/config/prompt.vue"),
        name: "prompt",
        meta: {
          title: "Prompt公共配置"
        }
      },
      {
        path: "one-to-one",
        component: () => import("@/views/config/one-to-one.vue"),
        name: "OneToOne",
        meta: {
          title: "问答对"
        }
      }
    ]
  },
  {
    path: "/tags",
    component: Layouts,
    redirect: "/tags",
    name: "tags",
    meta: {
      title: "标签管理",
      elIcon: "present"
    },
    children: [
      {
        path: "",
        component: () => import("@/views/tags/index.vue"),
        name: "tags",
        meta: {
          title: "标签管理"
        }
      }
    ]
  }
]

/**
 * 动态路由
 * 用来放置有权限 (Roles 属性) 的路由
 * 必须带有 Name 属性
 */
export const asyncRoutes: RouteRecordRaw[] = [
  {
    path: "/:pathMatch(.*)*", // Must put the 'ErrorPage' route at the end, 必须将 'ErrorPage' 路由放在最后
    redirect: "/404",
    name: "ErrorPage",
    meta: {
      hidden: true
    }
  }
]

const router = createRouter({
  history,
  routes: routeSettings.thirdLevelRouteCache ? flatMultiLevelRoutes(constantRoutes) : constantRoutes
})

/** 重置路由 */
export function resetRouter() {
  // 注意：所有动态路由路由必须带有 Name 属性，否则可能会不能完全重置干净
  try {
    router.getRoutes().forEach((route) => {
      const { name, meta } = route
      if (name && meta.roles?.length) {
        router.hasRoute(name) && router.removeRoute(name)
      }
    })
  } catch {
    // 强制刷新浏览器也行，只是交互体验不是很好
    window.location.reload()
  }
}

export default router
